<template>
  <div>
    <div class="setBox">
      <h3>
        <span>特训费缴纳设置</span>
      </h3>
      <div class="setContent" style="margin:20px 0;">
        <el-form ref="specialAmountSchoolView" :model="specialAmountSchoolView" label-width="150px">
          <el-form-item label="校区">
            <el-select v-model="specialAmountSchoolView.schoolId" @change="getSchoolSetUp" placeholder="请选择活动区域">
              <!-- <el-option label="全部" value=""></el-option> -->
              <el-option v-for="(a,index) in schoolList" :key="index" :label="a.schoolName" :value="a.schoolId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="小学插班生" v-if="schoolType==1">
            共收取
            <el-input-number
              v-model="specialAmountSchoolView.primarySchoolMoney"
              :controls="false"
              style="margin:0 10px;"
              :min="0"
              size="small"
              label="描述文字"
            ></el-input-number><el-tag style="margin:0 10px 0 0;">{{specialAmountSchoolView.heightSchoolMoney | hanStyle}}</el-tag>元
          </el-form-item>
          <el-form-item label="初中插班生" v-if="schoolType==2||schoolType==5">
            每学期收取
            <el-input-number
              v-model="specialAmountSchoolView.middleSchoolMoney"
              :controls="false"
              style="margin:0 10px;"
              :min="0"
              label="描述文字"
            ></el-input-number><el-tag style="margin:0 10px 0 0;">{{specialAmountSchoolView.middleSchoolMoney | hanStyle}}</el-tag>元
          </el-form-item>
          <el-form-item label="高中插班生" v-if="schoolType==3||schoolType==5">
            每学期收取
            <el-input-number
              v-model="specialAmountSchoolView.heightSchoolMoney"
              :controls="false"
              style="margin:0 10px;"
              :min="0"
              label="描述文字"
            ></el-input-number><el-tag style="margin:0 10px 0 0;">{{specialAmountSchoolView.heightSchoolMoney | hanStyle}}</el-tag>元
          </el-form-item>
          <el-form-item label="初升高（高一新生）" v-if="schoolType==3||schoolType==5">
            <ul class="fenshuduan">
              <li v-for="(q,index) in specialAmountSchoolView.highRangeList" :key="index">
                <el-input-number v-model="q.lowRange" :controls="false" :min="0"></el-input-number>~
                <el-input-number v-model="q.highRange" :controls="false" :min="q.lowRange+1" style="margin:0 20px 0 0;"></el-input-number>收取
                <el-input-number v-model="q.money" :controls="false" :min="0" style="margin:0 5px;"></el-input-number><el-tag style="margin:0 10px 0 0;">{{q.money | hanStyle}}</el-tag>元
                <el-button
                  type="success"
                  @click="addNewD(index)"
                  size="small"
                  icon="el-icon-plus"
                  circle
                ></el-button>
                <el-button v-if="index!=0" type="warning" @click="jian(index)" size="small" icon="el-icon-minus" circle></el-button>
              </li>
            </ul>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保存设置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "setUp",
  // TODO:数据内容
  data() {
    return {
      specialAmountSchoolView: {
        schoolId: "",
        primarySchoolMoney: 0,
        middleSchoolMoney: 0,
        heightSchoolMoney: 0,
        highRangeList:[
          {lowRange:0, highRange:0, money:0,}
        ],
      },
      schoolList:[],
      schoolType:0
    };
  },
  mounted() {
    this.getSchoolList();
  },
  filters:{
    hanStyle(money){
        var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //汉字的数字
          var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
          var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
          var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
          var cnInteger = "整"; //整数金额时后面跟的字符
          var cnIntLast = "元"; //整型完以后的单位
          var maxNum = 999999999999999.9999; //最大处理的数字
          var IntegerNum; //金额整数部分
          var DecimalNum; //金额小数部分
          var ChineseStr = ""; //输出的中文金额字符串
          var parts; //分离金额后用的数组，预定义
          var Symbol="";//正负值标记
          money = parseFloat(money);
          if (money==='') {
              return "";
          }
          if (money >= maxNum) {
              alert('超出最大处理数字');
              return "";
          }
          if (money == 0) {
              ChineseStr = cnNums[0] + cnIntLast + cnInteger;
              return ChineseStr;
          }
          if(money<0)
          {
              money=-money;
              Symbol="负 ";
          }
          money = money.toString(); //转换为字符串
          if (money.indexOf(".") == -1) {
              IntegerNum = money;
              DecimalNum = '';
          } else {
              parts = money.split(".");
              IntegerNum = parts[0];
              DecimalNum = parts[1].substr(0, 4);
          }
          if (parseInt(IntegerNum, 10) > 0) { //获取整型部分转换
              var zeroCount = 0;
              var IntLen = IntegerNum.length;
              for (var i = 0; i < IntLen; i++) {
                  var n = IntegerNum.substr(i, 1);
                  var p = IntLen - i - 1;
                  var q = p / 4;
                  var m = p % 4;
                  if (n == "0") {
                      zeroCount++;
                  }
                  else {
                      if (zeroCount > 0) {
                          ChineseStr += cnNums[0];
                      }
                      zeroCount = 0; //归零
                      ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
                  }
                  if (m == 0 && zeroCount < 4) {
                      ChineseStr += cnIntUnits[q];
                  }
              }
              ChineseStr += cnIntLast;
              //整型部分处理完毕
          }
          if (DecimalNum != '') { //小数部分
              var decLen = DecimalNum.length;
              for (var i = 0; i < decLen; i++) {
                  var n = DecimalNum.substr(i, 1);
                  if (n != '0') {
                      ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
                  }
              }
          }
          if (ChineseStr == '') {
              ChineseStr += cnNums[0] + cnIntLast + cnInteger;
          } else if (DecimalNum == '') {
              ChineseStr += cnInteger;
          }
          ChineseStr = Symbol +ChineseStr;
          return ChineseStr;

    }
  },
  methods: {
    // TODO:获取学校列表
    getSchoolList: function() {
      this.$api.trainingReduction.GetSpecialSrainingSchoolList().then(res => {
        //console.log("获取学校列表", res);
        this.schoolList = res.data.result;
        this.specialAmountSchoolView.schoolId = res.data.result[0].schoolId;
        this.schoolType = res.data.result[0].schoolTypeId;
        this.getSchoolSetUp();
      });
    },
    // TODO:加载学校下设置详情
    getSchoolSetUp: function(event) {
      console.log(event);
      for(var a=0,len = this.schoolList.length;a<len;a++){
        if(event==this.schoolList[a].schoolId){
          this.schoolType = this.schoolList[a].schoolTypeId;
        }
      }
      this.specialAmountSchoolView.primarySchoolMoney = "";
      this.specialAmountSchoolView.middleSchoolMoney = "";
      this.specialAmountSchoolView.heightSchoolMoney = "";
      this.specialAmountSchoolView.highRangeList = [{lowRange:0, highRange:0, money:0,}];
      this.$api.trainingReduction.GetSchoolSetUp(this.specialAmountSchoolView.schoolId).then(res=>{
        //console.log("获取学校下的设置",res);
        if(res.data.result){
          this.specialAmountSchoolView = res.data.result;
        }
      })
    },
    onSubmit() {
      this.$api.trainingReduction.SaveSchoolSetUp(this.specialAmountSchoolView).then(res=>{
        //console.log("保存学校设置",res)
        if(res.data.status=='SUCCESS'){
          this.$message({
            message:'特训费缴纳设置保存成功',
            type:'success'
          })
        }
        this.getSchoolSetUp();
      })
    },
    addNewD: function(suoyin) {
      var nobg = {
        lowRange: suoyin == 0 ? 0 : this.specialAmountSchoolView.highRangeList[suoyin - 1].highRange,
        highRange: 0,
        money: 0
      };
      this.specialAmountSchoolView.highRangeList.push(nobg);
    },
    jian:function(suoyin){
      this.specialAmountSchoolView.highRangeList.splice(suoyin,1);
    },
    //************************* 把数字金额转换成中文大写数字的函数(可处理负值) *********************************//
      changeNumMoneyToChinese(money){

      }




  },
  computed:{

  }
};
</script>

<style>
.fenshuduan li {
  padding: 10px 0;
}
.el-tag{ font-size: 14px; height: 40px; line-height: 40px; padding: 0 20px;}
</style>
